package nrb.utils
{
	import mx.utils.StringUtil;
	
	public dynamic class XMLHelper
	{
	    import flash.filesystem.*;
	    import mx.controls.Alert;
		private var fs_filestream:FileStream;
		private var str_dir:String = "ndx/";
		private var str_file:String = "default.xml";
		private var xml_data:XML;
		public function XMLHelper()
		{
			//TODO: implement function
		}
		public function func_check_fileexists(str_path:String):Boolean {
			fs_filestream = new FileStream();
			var fl_file:File = File.documentsDirectory;
			this.str_file = str_path;
			fl_file = fl_file.resolvePath(this.str_dir+this.str_file);
			return fl_file.exists;
		}
		public function func_open_xml(str_path:String):void {
			xml_data = XML("<errs><NotFound>"+str_dir+str_file+" not Found</NotFound></errs>");
			fs_filestream = new FileStream();
			this.str_file = str_path;
			var fl_file:File = File.documentsDirectory;
			fl_file = fl_file.resolvePath(this.str_dir+this.str_file);
			if (!fl_file.exists) {
			} else {
				fs_filestream.open(fl_file, FileMode.READ);
				xml_data = XML(fs_filestream.readUTFBytes(fs_filestream.bytesAvailable));
				fs_filestream.close();
			}
		}
		public function func_get_xml():XML {
			return this.xml_data;
		}
		public function func_create_xml(xml_source:XML, str_path:String):void {
			fs_filestream = new FileStream();
			this.str_file = str_path;
			var fl_file:File = File.documentsDirectory;
			fl_file = fl_file.resolvePath(this.str_dir+this.str_file);
			var str_xml:String =  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+xml_source.toXMLString();
			this.xml_data = xml_source;
			fs_filestream.open(fl_file, FileMode.WRITE);
			fs_filestream.writeUTFBytes(str_xml);
			fs_filestream.close();
		}
		public function func_write_xml(data:XML):void {
			var fl_file:File = File.documentsDirectory;
			fl_file = fl_file.resolvePath(str_dir+this.str_file);
			var str_xml:String =  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+data.toXMLString();
			fs_filestream.open(fl_file, FileMode.WRITE);
			fs_filestream.writeUTFBytes(str_xml);
			fs_filestream.close();
			this.func_open_xml(this.str_file);
		}
		public function func_cdata(str_name:String, str_value:String):XML {
			//str_value = StringUtil.substitute(
			return <{str_name}>{new XML("<![CDATA[" +str_value+ "]]>")}</{str_name}>;
		}
	}
}